Skip to content

Latest commit

 

History

History
65 lines (63 loc) · 2.01 KB

2-Basic Syntax.md

File metadata and controls

65 lines (63 loc) · 2.01 KB

Java Basic Syntax

Here are some basic syntax elements of the Java programming language:

1. File Structure

publicclassCodeGeek{ publicstaticvoidmain(String[] args) { System.out.println("Hello Coders!"); } }

The name of the file has to be the class name. Example : Here class name is CodeGeek therefore filename must have name as CodeGeek. Otherwise it will throw error.

2. Comments

Comments are used to add notes and explanations to your code. In Java, there are two types of comments: single-line comments and multi-line comments. Single-line comments start with // and extend to the end of the line. Multi-line comments start with /* and end with */ . Example

// This is a single-line comment/*This is amulti-line comment*/

3. Variables

Variables are used to store values in a program. In Java, you must declare a variable before you can use it and specify its data type. For example:

intx; // Declare an integer variable named xx = 10; // Assign the value 10 to xStringname; // Declare a string variable named namename = "John"; // Assign the value "John" to name

4. Data types

Java has several built-in data types, including integers (int), floating-point numbers (float and double), characters (char), and boolean values (boolean). For example:

intx = 10; // Integerfloatpi = 3.14; // Floating-point numbercharletter = 'A'; // Characterbooleanflag = true; // Boolean value

5. Case Sensitivity

Java is a case-sensitive language, which means that the language treats identifiers (such as variables, methods, and class names) that are written with different capitalization as different. For example, the variables x and X are treated as different identifiers in Java. The following code will result in an error:

intx = 5; System.out.println(X);

Output:

error: cannotfindsymbolSystem.out.println(X); ^ symbol: variableXlocation: classMain
close